/**
 * 
 */
package com.luck.pf.util;

import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 说明：从XLS文件中创建模板
 * Copyright: Copyright (c) 2009
 * 深圳络道科技
 * @author zhoucl
 * @version 1.0
 */
public abstract class XlsExcel {
	private static final String EXCEL_CONTENT_TYPE = "application/vnd.ms-excel";
	
	HSSFWorkbook workbook;
	
	int fromIdx;
	
	/**
	 * xls file 文件
	 * @param xlsFile
	 * @throws IOException 
	 * @throws IOException 
	 */
	public void build(java.io.InputStream xlsIs,List data,String sheetName) throws IOException{
		workbook = new HSSFWorkbook(xlsIs);
		buildHeader(null);
		buildBody(data);
		buildFoot();
		workbook.setSheetName(0, sheetName);
		xlsIs.close();
	}
	
	/**
	 * xls file 文件
	 * @param xlsFile
	 * @throws IOException 
	 * @throws IOException 
	 */
	public void build(java.io.InputStream xlsIs,List data,Object headData,String sheetName) throws IOException{
		workbook = new HSSFWorkbook(xlsIs);
		buildHeader(headData);
		buildBody(data);
		buildFoot();
		workbook.setSheetName(0, sheetName);
		xlsIs.close();
	}
	
	public HSSFSheet getSheetAt(int index){
		return workbook.getSheetAt(index);
	}
	
	abstract public void buildHeader(Object headData);
	abstract public void buildBody(List data);
	abstract public void buildFoot();

	public void createCell(HSSFRow row,int columIndex,Double content){
		HSSFCell cell = row.createCell((short)columIndex);
		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(content);
	}
	
	public void createCell(HSSFRow row,int columIndex,int content){
		if (content != 0) {
			HSSFCell cell = row.createCell((short) columIndex);
			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			cell.setCellValue(content);
		} else {
			HSSFCell cell = row.createCell((short) columIndex);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue("");
		}
	}
	
	public void createCell(HSSFRow row,int columIndex,String content){
		HSSFCell cell = row.createCell((short)columIndex);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(content);
	}
	
	/**
	 * @return the workbook
	 */
	public HSSFWorkbook getWorkbook() {
		return workbook;
	}


	/**
	 * @param workbook the workbook to set
	 */
	public void setWorkbook(HSSFWorkbook workbook) {
		this.workbook = workbook;
	}


	
	/**
	 * @return the fromIdx
	 */
	public int getFromIdx() {
		return fromIdx;
	}

	/**
	 * @param fromIdx the fromIdx to set
	 */
	public void setFromIdx(int fromIdx) {
		this.fromIdx = fromIdx;
	}

	/**
	 * @return the eXCEL_CONTENT_TYPE
	 */
	public static String getExcelContentType() {
		return EXCEL_CONTENT_TYPE;
	}
}
